using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace DIS_DAO.DAO
{
	#region using
	using System.Data;
	using DIS_DAO.DTO;
	using DIS_DAO.Helpers;
	#endregion

	/// <summary>
	/// Data access object, which get data of Customer table from database
	/// </summary>
	public class CustomerDAO : AbstractDAO
	{
		public CustomerDAO(string strPath): base(strPath)
		{
		}

		public DataSet SelectAll()
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Customer_SelectAll";
				base.FillDataSet(ref ds, "Customer");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public DataSet SelectByID(int id)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_Customer_SelectByID";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, id);
				base.FillDataSet(ref ds, "Customer");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public bool Insert(CustomerDTO obj)
		{
			try
			{
				base.sql = "usp_Customer_Insert";
				base.InitializeCommand();
				base.AddParameter("@Name", SqlDbType.NVarChar, 1000, obj.Name);
				base.AddParameter("@Phone", SqlDbType.NVarChar, 1000, obj.Phone);
				base.AddParameter("@Address", SqlDbType.NVarChar, 1000, obj.Address);
				base.AddParameter("@Email", SqlDbType.NVarChar, 1000, obj.Email);
				base.AddParameter("@UserName", SqlDbType.NVarChar, 1000, obj.UserName);
				base.AddParameter("@Password", SqlDbType.NVarChar, 1000, obj.Password);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public bool Update(CustomerDTO obj)
		{
			try
			{
				base.sql = "usp_Customer_Update";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, obj.ID);
				base.AddParameter("@Name", SqlDbType.NVarChar, 1000, obj.Name);
				base.AddParameter("@Phone", SqlDbType.NVarChar, 1000, obj.Phone);
				base.AddParameter("@Address", SqlDbType.NVarChar, 1000, obj.Address);
				base.AddParameter("@Email", SqlDbType.NVarChar, 1000, obj.Email);
				base.AddParameter("@UserName", SqlDbType.NVarChar, 1000, obj.UserName);
				base.AddParameter("@Password", SqlDbType.NVarChar, 1000, obj.Password);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				base.sql = "usp_Customer_Delete";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, id);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

        public DataSet SelectByUserName(string username)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_Customer_SelectByUserName";
                base.InitializeCommand();
                base.AddParameter("@UserName", SqlDbType.NVarChar, 1000, username);
                base.FillDataSet(ref ds, "Customer");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }

        public DataSet SelectByUserNameAndPassword(string username, string password)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_Customer_SelectByUserNameAndPassword";
                base.InitializeCommand();
                base.AddParameter("@UserName", SqlDbType.NVarChar, 1000, username);
                base.AddParameter("@Password", SqlDbType.NVarChar, 1000, password);
                base.FillDataSet(ref ds, "Customer");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }
	}
}